package IO操作;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class ReadStringDemo {
    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\tedu\\IdeaProjects\\JSD2302SE\\src\\main\\java\\IO操作\\ReadStringDemo.java");
        FileInputStream fis =new FileInputStream(file);
        //创建一个与txt文件一样长的字节数组
        byte[] data =new byte[(int)file.length()];
        //一次性将所有字节读取到数组中
        fis.read(data);
        //使用String的构造器可以将给定字节数组按照utf8编码转换为字符串
        String line =new String(data, StandardCharsets.UTF_8);
        System.out.println(line);
        fis.close();
    }
}
